<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>
        车位使用添加
    </title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="css/main.css" media="all">
    <script type="text/javascript" src="lib/loading/okLoading.js"></script>
    <style>
        .imgs {
            display: none;
        }

        .picture {
            display: none;
        }
    </style>
</head>
<body>

<div class="x-body">
    <form class="layui-form layui-form-pane" action="" id="add">
        <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">

            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                    <input type="hidden" name="id" id="id" value="">

                    <div class="layui-form-item">
                        <label class="layui-form-label" style="width: 180px">
                            <span class='x-red'>*</span>所属小区
                        </label>
                        <div class="layui-input-block" style="width: 300px;position: absolute;left: 105px">
                            <select name="communityId" lay-filter="changeCommunity" id="communityId">
                            </select>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label" style="width: 180px">
                            <span class='x-red'>*</span>车位编号
                        </label>
                        <div class="layui-input-block" style="width: 300px;position: absolute;left: 105px">
                            <select name="code" id="code">
                                <option value="0">---请选择车位编号---</option>
                            </select>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label" style="width: 180px">
                            <span class='x-red'>*</span>车辆所有人（业主）
                        </label>
                        <div class="layui-input-block" style="width: 300px;position: absolute;left: 105px">
                            <select name="ownerId" lay-filter="changeOwnerId" id="ownerId">
                                <option value="0">---请选择业主---</option>
                            </select>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label" style="width: 180px">
                            <span class='x-red'>*</span>业主车辆
                        </label>
                        <div class="layui-input-block" style="width: 300px;position: absolute;left: 105px">
                            <select name="carId" id="carId">
                                <option value="0">---请选择业主车辆---</option>
                            </select>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label" style="width: 180px">
                            <span class='x-red'>*</span>使用性质 (购买/出售)
                        </label>
                        <div class="layui-input-block">
                            <input type="radio" id="useTypes" name="useType" checked value="1" title="租售">
                            <div class="layui-unselect layui-form-radio"><i class="layui-anim layui-icon"></i>
                                <div>租售</div>
                            </div>

                            <input type="radio" id="useTypea" name="useType" value="0" title="购买">
                            <div class="layui-unselect layui-form-radio layui-form-radioed"><i
                                    class="layui-anim layui-icon layui-anim-scaleSpring"></i>
                                <div>购买</div>
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label" style="width: 180px">
                            <span class='x-red'>*</span>支付状态
                        </label>

                        <div class="layui-input-block">
                            <input type="radio" id="paymentStatusb" name="paymentStatus" checked value="2" title="未付款">
                            <div class="layui-unselect layui-form-radio layui-form-radioed"><i
                                    class="layui-anim layui-icon layui-anim-scaleSpring"></i>
                                <div>未付款</div>
                            </div>


                            <input type="radio" id="paymentStatusa" name="paymentStatus" value="1" title="已付款">
                            <div class="layui-unselect layui-form-radio"><i class="layui-anim layui-icon"></i>
                                <div>已付款</div>
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label" style="width: 180px">
                            <span class='x-red'>*</span>开始时间
                        </label>
                        <div class="layui-input-inline">
                            <input class="layui-input" placeholder="开始时间" name="startTime" id="startTime">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label" style="width: 180px">
                            <span class='x-red'>*</span>结束时间
                        </label>
                        <div class="layui-input-inline">
                            <input class="layui-input" placeholder="结束时间" name="endTime" id="endTime">
                        </div>
                    </div>


                    <div class="layui-form-item">
                        <label class="layui-form-label" style="width: 180px">
                            <span class='x-red'>*</span>总费用(￥)
                        </label>
                        <div class="layui-input-inline " style="width: 190px;">
                            <input type="text" value="500" readonly="true" style="background:#CCCCCC" name="totalFee"
                                   id="totalFee" placeholder="￥"
                                   class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <button class="layui-btn" type="button" lay-filter="add" lay-submit="">
                            保存
                        </button>
                    </div>
                    <!--</form>-->
                    <div style="height:100px;"></div>
                </div>


            </div>

        </div>
    </form>
</div>
<script src="js/layui-v2.7.6/layui/layui.js" type="text/javascript" charset="utf-8"></script>
<link rel="stylesheet" type="text/css" href="js/layui-v2.7.6/layui/css/layui.css"/>
<script src="js/jquery.js"></script>
<script src="js/x-layui.js" type="text/javascript" charset="utf-8"></script>
<script src="js/axios.js"></script>

<script>
    layui.use(['laydate', 'element', 'layer', 'form'], function () {
        lement = layui.element;//面包导航
        layer = layui.layer;//弹出层
        laydate = layui.laydate;
        var form = layui.form;
        okLoading.close($);
        var id = getQueryString("id");
        if (id == null) {
            getCommunity_id(null);
        } else {
            findById(id);
        }

        //日期初始化
        laydate.render({
            elem: "#startTime",
            type: "date"
            , istoday: false,
            min: getNowFormatDate()
        });

        laydate.render({
            elem: "#endTime",
            type: "date"
            , istoday: false
        });

        //监听提交
        form.on('submit(add)', function (data) {
            var community_id = $('#communityId option:selected').val();  //所属小区ID
            var code = $('#code option:selected').val();                  //车位编号
            var owner_id = $("#ownerId option:selected").val();          //车辆所有人（业主）ID
            // var image = $("input[name='image']").val();
            var car_id = $("#carId option:selected").val();              //车辆信息id
            var use_type = $("input[name='useType']:checked").val();     //使用性质：0-购买(默认)，1-租售
            var payment_status = $("input[name='paymentStatus']:checked").val(); //支付状态：1-已付款  2-未付款
            var total_fee = $("input[name='totalFee']").val();           //总费用(￥)，以分为单位
            var id = $("input[name='id']").val();
            var start_time = $("#startTime").val();         //开始时间
            var end_time = $("#endTime").val();             //结束时间
            if (community_id == "") {
                layer.msg('所属小区不能为空', {icon: 5, time: 2000});
                return false;
            }
            if (code == "") {
                layer.msg('车位编号不能为空', {icon: 5, time: 2000});
                return false;
            }
            if (car_id == "") {
                layer.msg('车辆信息不能为空', {icon: 5, time: 2000});
                return false;
            }
            if (owner_id == "") {
                layer.msg('车辆所有人不能为空', {icon: 5, time: 2000});
                return false;
            }
            // if (payment_status == "") {
            //     layer.msg('联系电话不能为空', {icon: 5, time: 2000});
            //     return false;
            // }
            if (start_time == "") {
                layer.msg('开始时间不能为空', {icon: 5, time: 2000});
                return false;
            }
            if (end_time == "") {
                layer.msg('截止时间不能为空', {icon: 5, time: 2000});
                return false;
            }
            // var parkingUse=data.field
            console.log("../parkingUse/" + (id == null ? "add" : "update") + "?communityId=" + community_id + "&code=" + code + "&carId=" + car_id + "&ownerId=" + owner_id + "&startTime=" + start_time + "&endTime=" + end_time + "&paymentStatus=" + payment_status + "&useType=" + use_type);
            $.ajax({
                type: "post",
                url: "../parkingUse/" + (id == null ? "add" : "update") + "?communityId=" + community_id + "&code=" + code + "&carId=" + car_id + "&ownerId=" + owner_id + "&startTime=" + start_time + "&endTime=" + end_time + "&paymentStatus=" + payment_status + "&useType=" + use_type,
                // url: "../parkingUse/add",
                // data: parkingUse;
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    //alert(1);
                    if (data.status == 2000) {
                        layer.msg(data.info, {icon: 6, time: 2000}, function () {
                            window.parent.location.reload();
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                        });
                        return false;
                    } else {
                        layer.msg(data.info, {icon: 5, time: 2000});
                        return false;
                    }
                }
            });
        });

        form.on('select(changeCommunity)', function () {
            var community_id = $('#communityId option:selected').val();  //所属小区ID
            if (community_id == 0) {
                getCodeAndOwnerId();
            } else {
                getCode(community_id);
                getOwnerId(community_id);
            }
        })

        form.on('select(changeOwnerId)', function () {
            var ownerId = $('#ownerId option:selected').val();  //所属小区ID
            if (ownerId == 0) {
                $("#carId").html("");
                var carId = "<option value='0'>---请选择业主车辆---</option>";
                $("#carId").append(carId);
                layui.form.render("select");
            } else {
                getCar(ownerId);
            }
        })
    })

    function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var seperator2 = ":";
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = date.getFullYear() + seperator1 + month
            + seperator1 + strDate + " " + date.getHours() + seperator2
            + date.getMinutes() + seperator2 + date.getSeconds();
        return currentdate;
    }

    function findById(id) {
        axios.post("../parkingUse/findById?id=" + id).then((res) => {
            var data = res.data.data;
            $("#id").val(data.id);
            $("#totalFee").val(data.totalFee);
            $("#startTime").val(layui.util.toDateString(data.startTime, 'yyyy-MM-dd'));
            $("#endTime").val(layui.util.toDateString(data.endTime, 'yyyy-MM-dd'));
            getCode(data.communityId, data.code);
            getCommunity_id(data.communityId);
            getOwnerId(data.communityId, data.ownerId);
            getCar(data.ownerId, data.carId);
            console.log(data);
            $('#carId').attr("disabled", true);
            $('#ownerId').attr("disabled", true);
            $('#communityId').attr("disabled", true);
            $('#code').attr("disabled", true);
            $('#startTime').attr("disabled", true);
            $('#endTime').attr("disabled", true);
            if (data.useType == "购买") {

                $("#useTypes").attr("checked", "checked");
            } else {
                $("#useTypes").attr("checked", "checked");
            }
            if (data.paymentStatus == "已付款") {
                $("#paymentStatusa").attr("checked", "checked");
            } else {
                $("#paymentStatusb").attr("checked", "checked");
            }

        });
        document.getElementById("useTypes").disabled = true;
        document.getElementById("useTypea").disabled = true;
        document.getElementById("paymentStatusa").disabled = true;
        document.getElementById("paymentStatusb").disabled = true;
        layui.form.render("radio");
    }

    //车位编号
    function getCode(community_id, code) {
        $.ajax({
            type: "GET",//请求类型
            url: "../parking/selectByCommunityId?communityId=" + community_id,//请求的urlqh
            dataType: "json",//ajax接口（请求url）返回的数据类型
            success: function (data) {//data：返回数据（json对象）
                var options = "<option value='0'>---请选择车位编号---</option>";
                for (var i = 0; i < data.data.length; i++) {
                    if (code == data.data[i].code) {
                        options += "<option selected value=\"" + data.data[i].id + "\" class>" + data.data[i].code + "</option>";
                    } else {
                        options += "<option value=\"" + data.data[i].id + "\" class>" + data.data[i].code + "</option>";
                    }
                }
                $("#code").html("");
                $("#code").append(options);
                layui.form.render("select");
            },
            error: function (data) {//当访问时候，404，500 等非200的错误状态码
                alert("加载车位编号失败！");
            }
        });
    }

    //车辆所有人
    function getOwnerId(community_id, ownerId) {
        $.ajax({
            type: "GET",//请求类型
            url: "../owner/selectByCommunityId?communityId=" + community_id,//请求的urlqh
            dataType: "json",//ajax接口（请求url）返回的数据类型
            success: function (data) {//data：返回数据（json对象）
                var options = "<option value='0'>---请选择车辆所有人---</option>";
                $("#ownerId").html("");
                for (var i = 0; i < data.data.length; i++) {
                    if (ownerId == data.data[i].id) {
                        options += "<option selected value=\"" + data.data[i].id + "\" class>姓名：" + data.data[i].name + "     身份证号：" + data.data[i].idcard.substring(0, 5) + "*********" + data.data[i].idcard.substring(data.data[i].idcard.length - 5, data.data[i].idcard.length) + "</option>";
                    } else {
                        options += "<option value=\"" + data.data[i].id + "\" class>姓名：" + data.data[i].name + "     身份证号：" + data.data[i].idcard.substring(0, 5) + "*********" + data.data[i].idcard.substring(data.data[i].idcard.length - 5, data.data[i].idcard.length) + "</option>";
                    }
                }
                $("#ownerId").append(options);
                $("#carId").html("");
                var car = "<option value='0'>---请选择业主车辆---</option>";
                $("#carId").append(car);
                layui.form.render("select");
            },
            error: function (data) {//当访问时候，404，500 等非200的错误状态码
                alert("加载车辆所有人失败！");
            }
        });
    }

    //业主车辆
    function getCar(ownerId, carId) {
        $.ajax({
            type: "GET",//请求类型
            url: "../car/selectByOwnerId?ownerId=" + ownerId,//请求的urlqh
            dataType: "json",//ajax接口（请求url）返回的数据类型
            success: function (data) {//data：返回数据（json对象）
                var options = "<option value='0'>---请选择业主车辆---</option>";
                for (var i = 0; i < data.data.length; i++) {
                    if (carId == data.data[i].id) {
                        options += "<option selected value=\"" + data.data[i].id + "\" class>" + data.data[i].carNumber + "</option>";
                    } else {
                        options += "<option value=\"" + data.data[i].id + "\" class>" + data.data[i].carNumber + "</option>";
                    }
                }
                $("#carId").html("");
                $("#carId").append(options);
                layui.form.render("select");
            },
            error: function (data) {//当访问时候，404，500 等非200的错误状态码
                alert("加载业主车辆失败！");
            }
        });
    }


    //回显
    function getCodeAndOwnerId() {
        var community_id = $('#communityId option:selected').val();  //所属小区ID
        if (community_id == 0) {
            $("#ownerId").html("");
            var ownerId = "<option value='0'>---请选择车辆所有人---</option>";
            $("#ownerId").append(ownerId);


            $("#code").html("");
            var code = "<option value='0'>---请选择车位编号---</option>";
            $("#code").append(code);


            $("#carId").html("");
            var carId = "<option value='0'>---请选择业主车辆---</option>";
            $("#carId").append(carId);
            layui.form.render("select");

        } else {
            getCode(community_id);
            getOwnerId(community_id);
        }
    }


    //所属小区
    function getCommunity_id(community_id) {
        $.ajax({
            type: "GET",//请求类型
            url: "../community/find",//请求的urlqh
            dataType: "json",//ajax接口（请求url）返回的数据类型
            success: function (data) {//data：返回数据（json对象）
                var options = "<option value='0'>---请选择所属小区---</option>";
                $("#communityId").html("");
                layui.form.render("select");
                for (var i = 0; i < data.data.length; i++) {
                    if (community_id == data.data[i].id && community_id != null) {
                        options += "<option selected value=\"" + data.data[i].id + "\" class>" + data.data[i].name + "</option>";
                    } else {
                        options += "<option value=\"" + data.data[i].id + "\" class>" + data.data[i].name + "</option>";
                    }
                }
                $("#communityId").append(options);
                layui.form.render("select");
            },
            error: function (data) {//当访问时候，404，500 等非200的错误状态码
                alert("加载所属小区失败！");
            }
        });
    }


    function getQueryString(name) {
        let reg = `(^|&)${name}=([^&]*)(&|$)`
        let r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }


</script>


<!--栏目缩略图上传-->
<script>
    function upload(obj, id) {
        var formData = new FormData();
        formData.append('images', $('#previewImg')[0].files[0]);
        //console.log(formData);
        formData.append('id', id);
        layer.msg('图片上传中', {icon: 16});
        $.ajax({
            type: "post",
            processData: false,
            contentType: false,
            url: "xxx",
            data: formData,
            success: function (data) {
                if (data.status == 1) {
                    console.log(data.image_name);
                    layer.closeAll('loading');
                    //layer.msg(data.info,{icon:1,time:1000});
                    $("#pimages").attr('src', data.image_name);
                    $("#avatar").val(data.image_name);
                    $(".imgs").show();
                    return false;
                } else {
                    layer.msg(data.info, {icon: 2, time: 1000});
                }
            }
        });
    }
</script>

</body>
</html>